enum MemberType {
  quanzhi = 2,
  jianzhi = 3,
  all = 1
}
enum MemberState {
  man = 1,
  xian = 2
}

interface Member {
  id: number;
  name: string;
  image: '';
  state: MemberState.man;
  type: MemberType.quanzhi;
  check: boolean
}

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: true
    },
    mode: {
      type: String,
      value: 'select', // page 页面模式 select选择器模式
    },
    type: {
      type: Number, // 1代表全部，2代表专职 3代表兼职
      value: 1,
    },
    height: {
      type: String, // 自定义高度
      value: ''
    },
    // 初始化选中的值
    selectedIds: {
      type: Array,
      value: [] as number[],
      observer(list: number[] = []) {
        if (!list.length) return;
        list.forEach((f) => this.data.list.some((s) => f === s.id && (s.check = true)))
        this.setData({ list: this.data.list })
        this.setData({ selectTotal: list.length })
        this.refreshData();
      }
    }
  },



  /**
   * 组件的初始数据
   */
  data: {
    triggered: false,
    loading: false,
    list: [
      { id: 1, name: '里大炮1', state: 1, image: '', type: 1, },
      { id: 2, name: '里大炮2', state: 2, image: '', type: 1, },
      { id: 3, name: '里大炮3', state: 1, image: '', type: 1, },
      { id: 4, name: '里大炮', state: 2, image: '', type: 2, },
      { id: 6, name: '里大炮', state: 1, image: '', type: 2, },
      { id: 7, name: '里大炮', state: 2, image: '', type: 2, },
      { id: 8, name: '里大炮', state: 1, image: '', type: 1, },
    ] as Member[],
    viewList: [] as Member[],
    types: [{ label: '全部', id: 1 }, { label: '专职', id: 2 }, { label: '兼职', id: 3 }], // 人员分类
    selectTotal: 0,
    show: false,
    search: ''
  },
  observers: {
    search() {
      this.refreshData()
    }
  },

  lifetimes: {
    attached() {
      this.refreshData()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.setData({ show: false })
    },
    onActiveType(e: any) {
      const id = e.currentTarget.dataset.id;
      this.setData({ type: id })
    },
    /**
     * 选中项
     */
    onChooseItem(e: any) {
      const id = e.currentTarget.dataset.id as number;
      const item = this.data.list.find((f) => f.id === id);

      // 页面模式
      if (this.properties.mode === 'page') {
        this.triggerEvent("choose", item)
        return;
      }

      // 选择器模式
      if (id) {
        let item: Member | null = null;
        this.data.list.some((s) => {
          if (s.id === id) {
            s.check = !s.check
            item = s;
            return true;
          }
          return false;
        })
        const allItems = this.data.list.filter((f) => f.check);
        this.triggerEvent("choose", { allItems, currItem: item });
        this.setData({ list: this.data.list });
        this.setData({ selectTotal: allItems.length })
        this.refreshData();
      }

    },
    /**
     * 删除项
     */
    onDelItem(e: any) {
      const id = e.currentTarget.dataset.id;
      const del = () => {
        this.setData({ list: this.data.list.filter(f => f.id !== id) })
      }
      wx.showModal({
        title: '提示',
        content: '移除人员，将不可参与订单',
        success(res) {
          if (res.confirm) {
            del();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    },
    /**
     * 刷新显示数据
     */
    refreshData() {
      const list = this.data.list.filter((f) => {
        if (this.properties.type !== MemberType.all) {
          if (f.type !== this.properties.type) return false;
        }
        if (!this.data.search) return true;

        return f.name.includes(this.data.search);
      })

      this.setData({ viewList: list });
      console.log("listxx", this.data.list, this.data.viewList);

    }

  }
})